/**
 * 主功能页面
 * author:zhushy
 * date:2016年3月9日21:30:42
 */
;
define(function(require, exports, module) {
	var wechat = require("wechat");
	var template = require("template");
	var iscroll = require("iscroll");
	var index = {
		data: {},
		wxUserInfoObj: {},
		recommEnable: true,
		/**
		 * bindEvent 页面事件
		 */
		bindEvent: function() {
			//			$("#activity_index").unbind().on("click", function() {
			//				window.location.href = "#/index/activityList";
			//			});
			$(document).off("click", '#equity_index').on("click", '#equity_index', function() {
				window.location.href = "#/index/equity";
			});
			$("#signIn_index").unbind().on("click", function() {
				window.location.href = "#/index/signIn";
			});
			$(document).off("click", '.list_info').on("click", '.list_info', function() {
				var locateAttr = $(this).attr("locate");
				window.location.href = "#/index/" + locateAttr;
			});
			$(document).off("click", '#tuijian_index').on("click", '#tuijian_index', function() {
				if(index.recommEnable) {
					window.location.href = "#/index/shareActivity";
				}
			});
			$(document).off("click", '#notice_index').on("click", '#notice_index', function() {
				window.location.href = "#/index/register";
			});
			$("#integral_index").unbind().on("click",function(){
				var mob = index.wxUserInfoObj.data.mobile;
				var wscurlstr =index.wxUserInfoObj.data.inlUrl;
				if(mob && mob.length==11){
					if(wscurlstr&& wscurlstr.length>0){
						var appid = window.localStorage.getItem("appId");
						var openid = window.localStorage.getItem("openId");
						wscurlstr=decodeURIComponent(wscurlstr);
						var wscurl = wscurlstr + "?openId=" + openid +"&phoneNumber="+mob+"&type=weihuiyuan";
						window.location.href=wscurl;
					}else{
						common.showTip({
							content: "没有配置积分换礼链接，请联系管理员"
						});
					}
				}else{
					window.location.href = "#/index/register";
				}
			});
			$("#payTo_index").unbind().on("click",function(){
				var isPayPwdEnable= index.wxUserInfoObj.data.isPayPwdEnable;
				if(isPayPwdEnable){
					//启用支付密码
					var hasPayPwd = index.wxUserInfoObj.data.hasPayPwd;
					if(hasPayPwd){
						//有支付密码 判断是否记住密码
						var isRememberPwd =index.wxUserInfoObj.data.isRememberPwd;
						if(isRememberPwd){
							//记住支付密码 直接打开
							window.location.href = "#/index/payto";
						}else{
							$(".pwdPart").show();
							$("#hideinput_index").focus();
							index.initPwdPart();							
						}
					}else{
						//无支付密码 去设置密码
						var r=confirm("温馨提示：为了保证您钱包账户的安全，请先设置支付密码，再使用钱包授权线下支付！");
						if(r){
							window.localStorage.setItem("loadType","type0");
							window.location.href = "#/index/setPwd";
						}
					}	
				}else{
					//未启用支付密码
					window.location.href = "#/index/payto";
				}
//				
			});
		},
		/**
		 * 进行页面路由监听配置
		 * 
		 */
		bindRout: function() {
			//公共页面监听路由
			Path.map("#/index/:page_name").to(function() {
				console.log(this.params["page_name"]);
				var page_name = this.params["page_name"];
				var nextPageFrom = "right"; //从右边过来
				//页面加载方向
				if(page_name == "nearShop" || page_name == "home" || page_name == "my") {
					nextPageFrom = "none";
				}
				//记录状态
				common.loadPage({
					pageUrl: "html/" + page_name + ".html",
					reload: true,
					from: nextPageFrom
				});
			});
			//首页监听路由
			Path.map("#home").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: false,
					from: "left"
				});
			});
			//公共页面监听路由
			Path.map("").to(function() {
				//返回首页
				common.loadPage({
					pageUrl: "html/index.html",
					reload: false,
					from: "left"
				});
			});
			//Path.root("#home");
			Path.listen();
		},
		/**
		 * 页面数据加载
		 */
		initData: function() {
			$(".pwdPart").hide();
			window.location.href = "#home";
			var appid = window.localStorage.getItem("appId");
			var openid = window.localStorage.getItem("openId");
			
			var param = {

			};
			common.loadData({
				url: "/webchat/getuserinfo?appid=" + appid + "&openid=" + openid, //"/UMPPubServlet?method=getWxUserInfo",
				param: param,
				successCallBack: function(res) {
					if(res.status == "1") {
						var param = {
							"openId": openid,
							"appId": appid,
							"nickname":res.data.nickname,
							"sex":res.data.sex,
							"country":res.data.country,
							"province":res.data.province,
							"city":res.data.city
						}
						common.loadData({
							url: "/wxmember/service/getWxUserInfo", //"/UMPPubServlet?method=getWxUserInfo",
							param: param,
							successCallBack: index.getWxUserInfoCallBack, //回调函数
							dataSource: "getWxUserInfo",
							cache: false
						});
						var headImgUrl = res.data.headImgUrl;
						window.localStorage.setItem("headImgUrl", headImgUrl);
						$("#abbr_my_index").attr("src", headImgUrl);
						$("#name_index").text(res.data.nickname);
						window.localStorage.setItem("nickname", res.data.nickname);
					}else{
						common.showTip({
							content: "获取微信用户信息出错"
						});
					}
				}, //回调函数
				cache: false
			});

		},

		//callback function
		getWxUserInfoCallBack: function(json) {
			console.log(JSON.stringify(json));
			if(json.status == "0") {
				window.localStorage.setItem("WxUserInfo", JSON.stringify(json));
				index.wxUserInfoObj = json;
				var moblie = json.data.mobile;
				if(moblie && moblie.length == 11) {
					$("#notice_index").hide();
				} else {
					$("#notice_index").show();
				}
				$(".nex-tit-right").text(json.data.upgradeTips);
				$("#couponNumber").text(json.data.couponNumber);
				$("#myPoints").text(json.data.myPoints);
				$("#balance").text(json.data.balance);
				$("#currentLeal_index").text(json.data.currentLevel);
				$("#credit_index").text(json.data.myPoints);
				//推荐和签到
				if(json.data.recommEnable == "true") {
					index.recommEnable = true;
					$("#tuijianNotice").hide();
					$("#tuijian_img").attr("src", "images/tuijian.jpg");
				} else {
					index.recommEnable = false;
					$("#tuijianNotice").show();
					$("#tuijian_img").attr("src", "images/waiting.jpg");
				}
				if(json.data.signInEnable == "true") {
					$("#signIn_index").show();
				} else {
					$("#signIn_index").hide();
				}

				var html1 = template('levelTemplate', json);
				$('#iscrollContent_index2').html(html1);

				var window_width = document.body.clientWidth * 0.96;
				var length = json.data.allLevel.length;
				var cellwidth = window_width / length;
				$(".lip").css("width", cellwidth);
				//				$('.bar').css("width","96%");
				for(var i = 0; i < json.data.allLevel.length; i++) {
					if(json.data.currentLevelCode == json.data.allLevel[i].levelCode) {
						var width = cellwidth * (i + 0.5);
						var dotleft = width - 7;
						$('.bar1').css("width", width);
						$(".dot_index").css("left", dotleft);
						var bar2width = window_width - width - 1;
						$('.bar2').css("width", bar2width);
					}
				}
				//				$('.bar2').width(widthfull - widthbar1);
				//					common.initIscroll({
				//						container: "iscrollContent_index2", //scroll对应的位置
				//						showScrollBar: false,
				//						isVertical: false,
				//						index: 2,
				//						scrollWidth: width
				//					});
				//				} else {
				////					window.location.href = "#/index/collectCard" ;
				//				}
			} else {
				common.showTip({
					content: json.message
				});
			}
			common.initIscroll({
				container: "iscrollContent_index",
				showScrollBar: false,
				index: 1
			});

		},
		/**
		 * 支付密码部分
		 */
		initPwdPart:function(){
			//弹出密码输入框
			
			$("#hideinput_index").on("input", function() {
				index.inputPwd($("#hideinput_index").val());
			});
			$(".pwdPart").unbind().on("click",function(){
				$("#hideinput_index").focus();
			});
			$(".pwd_fg_index").unbind().on("click",function(){
				$("#hideinput_index").blur();
				window.localStorage.setItem("loadType","type3");
			    window.location.href = "#/index/setPwd";
			});
			$("#close_div").unbind().on("click",function(){
				$(".pwdPart").hide();
				$("#hideinput_index").blur();
			});
			$("#Pwdindexbtn").unbind().on("click",function(){
				//验证密码
				var str = $("#hideinput_index").val();
			   if(str &&str.length == 6) {
			   		//是否记住密码
					var isremember=$("#rem_pwd_index").is(":checked");
					var bool = isremember?"true":"false";
					//验证密码				
					var param = {
						"openId": window.localStorage.getItem("openId"),
						"appId": window.localStorage.getItem("appId"),
						"payPassword":str,
						"isRemember":bool
					};
					common.loadData({
						url: "/wxmember/service/verifyPayPassword",
						param: param,
						successCallBack: function(res) {
							if(res.status == "0") {
								$("#hideinput_index").blur();
								//密码正确
								window.location.href = "#/index/payto";
							} else {
								common.showTip({
									content: "密码输入错误，请重新输入！"
								}); //密码验证错误出错
								$("#hideinput_index").val("");
								$("#hideinput_index").focus();
								$(".pwd_detail_index").text("");
							}
						}, //回调函数
						cache: false //标识是不加载全部
					});

				} else {
					common.showTip({
						content: "请输入六位数字密码！"
					});
				}
			});
		},
		//密码框显示*
		inputPwd:function(str){
			$(".pwd_detail_index").text("");
			for(var i = 0; i < str.length; i++) {
				$("#Pwdindex" + i).text("*");
			}
			if(str.length==6){
				$("#Pwdindexbtn").click();
			}
		},
		
		/**
		 * each page init mothed
		 */
		initialize: function() {
			//			window.localStorage.setItem("openId", "oZ6swt3KqZdhdl3oBvIqPCBBZI8U");
			//			window.localStorage.setItem("appId", "wx0e9a5326968669b3");
			//动态加载数据
			index.initData();
			//事件绑定
			index.bindEvent();
			//路由绑定
			index.bindRout();
		}
	};

	module.exports = index;
});